package com.wnhz.work.jy.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.solidfire.gson.Gson;
import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.MyMain;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.AssetsAdapter;
import com.wnhz.work.jy.bean.AssetsBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.DensityUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;
import com.wnhz.work.jy.view.MyScrollView;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资产
 */
public class AssetsActivity extends BaseActivity implements View.OnClickListener, MyScrollView.OnScrollListener {
    private static final String TAG = "AssetsActivity";
    private ListView listView;
    //private List<GlodBean> list = new ArrayList<>();
    private List<AssetsBean.MyGoods> list = new ArrayList<>();
    private AssetsAdapter adapter;
    private MyScrollView scrollView;

    private AssetsBean assetsBean;

    @ViewInject(R.id.tv_title)
    private TextView tv_title;
    @ViewInject(R.id.include_occupied)
    private View include_occupied;
    @ViewInject(R.id.blank_img)
    private ImageView blank_img;
    @ViewInject(R.id.blank_tv)
    private TextView blank_tv;

    @ViewInject(R.id.tv_money)
    private TextView tv_money;
    @ViewInject(R.id.tv_score)
    private TextView tv_score;
    private View layout_main_title;

    private int width;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assets);
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        width = wm.getDefaultDisplay().getWidth();
        x.view().inject(this);
        WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        int max_height = display.getHeight();
        int fixed_height = DensityUtils.dp2px(this, 202);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) include_occupied.getLayoutParams();
        params.height = max_height - fixed_height;
        include_occupied.setLayoutParams(params);
        layout_main_title = findViewById(R.id.layout_main_title);

        scrollView = (MyScrollView) findViewById(R.id.scrollView);
        listView = (ListView) findViewById(R.id.listView);
        findViewById(R.id.btn_main_left).setVisibility(View.VISIBLE);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.tv_topup).setOnClickListener(this);
        findViewById(R.id.tv_extract).setOnClickListener(this);
        /*for (int i = 0; i < 4; i++) {
            GlodBean b = new GlodBean();
            list.add(b);
        }*/

        adapter = new AssetsAdapter(this, list, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = (int) view.getTag();
                if ("1".equals(list.get(i).getBuy_back())) {
                    if ("1".equals(MyApplication.getInstance().userBean.getPayPassword())) {
                        startActivity(new Intent(AssetsActivity.this, SaleGoldenActivity.class).putExtra("MyGoods",
                                list.get(i)));
                    } else {
                        showDialogv7(R.string.pay_toast, "取消", "去设置", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                startActivity(new Intent(AssetsActivity.this, PayPasswordActivity.class));
                            }
                        });
                    }
                }

            }
        });
        listView.setAdapter(adapter);
        setBlank();
        scrollView.smoothScrollTo(0, 0);
        scrollView.setOnScrollListener(this);
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            private int lastY = 0;
            private int touchEventId = -9983761;
            Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    View scroller = (View) msg.obj;
                    if (msg.what == touchEventId) {
                        if (lastY == scroller.getScrollY()) {
                            handleStop(scroller);
                        } else {
                            handler.sendMessageDelayed(handler.obtainMessage(touchEventId, scroller), 10);
                            lastY = scroller.getScrollY();
                        }
                    }
                }
            };


            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    handler.sendMessageDelayed(handler.obtainMessage(touchEventId, v), 10);
                }
                return false;
            }


            private void handleStop(Object view) {
                ScrollView scroller = (ScrollView) view;
                int scrollY = scroller.getScrollY();
                Log.e(TAG, "handleStop: " + scrollY);
                setcolor(scrollY);
            }
        });
    }

    @Override
    protected void onResume() {
        getData();
        super.onResume();
    }

    private void setBlank() {
        if (list.size() == 0) {
            include_occupied.setVisibility(View.VISIBLE);
        } else {
            include_occupied.setVisibility(View.GONE);
        }
    }

    @Event(value = {R.id.tv_addAddress})
    private void event(View view) {
        switch (view.getId()) {
            case R.id.tv_addAddress:
                startActivity(new Intent(AssetsActivity.this, MyMain.class).putExtra("Pagination", 2));
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.tv_topup:
                if ("1".equals(MyApplication.getInstance().userBean.getPayPassword())) {
                    startActivity(new Intent(this, TopUpActivity.class));
                } else {
                    showDialogv7(R.string.pay_toast, "取消", "去设置", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startActivity(new Intent(AssetsActivity.this, PayPasswordActivity.class));
                        }
                    });
                }

                break;
            case R.id.tv_extract:
                if ("1".equals(MyApplication.getInstance().userBean.getPayPassword())) {
                    startActivity(new Intent(this, WithdrawDepositActivity.class));
                } else {
                    showDialogv7(R.string.pay_toast, "取消", "去设置", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startActivity(new Intent(AssetsActivity.this, PayPasswordActivity.class));
                        }
                    });
                }

                break;
        }
    }

    private void getData() {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        showDialog();
        XUtil.Post(ConfigUtil.MYASSETS, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.d(TAG, "onSuccess() returned: " + result);
                assetsBean = new Gson().fromJson(result, AssetsBean.class);
                list.clear();
                list.addAll(assetsBean.getGoods());
                tv_money.setText(assetsBean.getAssets());
                tv_score.setText(assetsBean.getIntegral());
                adapter.notifyDataSetChanged();
                setBlank();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }


    private void setcolor(int scrollY) {
        double layout_main_title_height = layout_main_title.getHeight() * 1.00;
        double maxHeight = layout_main_title_height * 2;
        if (scrollView.getHeight() > width + maxHeight) {
            if (0 < scrollY && scrollY < maxHeight) {
                double max = scrollY / maxHeight;
                String a = Integer.toHexString((int) (max * 255));
                String my0x;
                if (a.length() == 0) {
                    my0x = "00";
                } else if (a.length() == 1) {
                    my0x = "0" + a;
                } else {
                    my0x = a;
                }
                String s = "#" + my0x + "ffffff";
                layout_main_title.setBackgroundColor(Color.parseColor(s));
                tv_title.setTextColor(0x00666666);
            } else if (0 >= scrollY) {
                tv_title.setTextColor(0x00666666);
                layout_main_title.setBackgroundColor(Color.parseColor("#00ffffff"));
            } else {
                tv_title.setTextColor(0xff666666);
                layout_main_title.setBackgroundColor(Color.parseColor("#ffffffff"));
            }
        }

    }

    @Override
    public void onScroll(int scrollY) {
        setcolor(scrollY);
    }
}

